﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagement.BL;
using System.Text.RegularExpressions;
using HotelManagement.Common;

namespace HotelManagement.PL
{
    public partial class GoodsMngForm : Form
    {
        private Goods m_GoodsInfo;
        private bool m_bIsUpdate;

        public Goods GoodsInfo
        {
            get { return m_GoodsInfo; }
            set { m_GoodsInfo = value; }
        }


        public GoodsMngForm(bool isUpdate)
        {
            InitializeComponent();
            m_bIsUpdate = isUpdate;
        }
        private GoodsMngBL goodsMng = new GoodsMngBL();


        private void SaleForm_Load(object sender, EventArgs e)
        {
            if (m_bIsUpdate)
            {
                this.Text = "维护货物";
                labTitle.Text = "维护货物";
            }
            else
            {
                this.Text = "添加货物种类";
                labTitle.Text = "添加货物种类";
                txtName.Enabled = true;
            }

            if (null != m_GoodsInfo) 
            {
                txtName.Text = m_GoodsInfo.Name;
                txtPurchase.Text = m_GoodsInfo.PurchasePrice.ToString();
                txtSale.Text = m_GoodsInfo.SalePrice.ToString();
                txtWaring.Text = m_GoodsInfo.WaringCount.ToString();
                txtDescription.Text = m_GoodsInfo.Description;
            }
        }

        
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) 
            {
                return;
            }
            if (m_bIsUpdate)
            {
                UpdateGoods();
            }
            else 
            {
                AddGoods(); 
            }
        }

        private bool CheckInput() 
        {
            bool bReturn = true;
            string strIntPattern = @"\d+$";
            string strFloatPattern = @"^(0|[1-9]\d*)$|^(0|[1-9]\d*)\.(\d{1,2})$";

            if (!m_bIsUpdate) 
            {
                if (string.IsNullOrEmpty(txtName.Text.Trim())) 
                {
                    MessageBox.Show("请输入货物名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }

            if (!Regex.IsMatch(txtPurchase.Text, strFloatPattern))
            {
                MessageBox.Show("买入价格输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (!Regex.IsMatch(txtSale.Text, strFloatPattern))
            {
                MessageBox.Show("卖出价格输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (!Regex.IsMatch(txtWaring.Text, strIntPattern))
            {
                MessageBox.Show("警告数量输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return bReturn;
        }

        private void UpdateGoods() 
        {
            if (0 <= goodsMng.UpdateGoods(buildGoodsInfo()))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateParent();
                this.Close();
            }
            else
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void AddGoods() 
        {
            if (0 <= goodsMng.AddGoods(buildGoodsInfo()))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                UpdateParent();
                this.Close();
            }
            else
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private Goods buildGoodsInfo() 
        {
            Goods goods = new Goods();
            if (m_GoodsInfo != null) 
            {
                goods.ID = m_GoodsInfo.ID;
            }
            goods.Name = txtName.Text;
            goods.PurchasePrice = Convert.ToSingle(txtPurchase.Text);
            goods.SalePrice = Convert.ToSingle(txtSale.Text);
            goods.WaringCount = Convert.ToInt32(txtWaring.Text);
            goods.Description = txtDescription.Text;
            return goods;
        }

        private void UpdateParent()
        {
            SearchGoodsTypeForm searchFrom = null;
            foreach (Form frm in this.Owner.MdiChildren)
            {
                if (frm.GetType() == typeof(SearchGoodsTypeForm))
                {
                    searchFrom = (SearchGoodsTypeForm)frm;
                }
            }
            if (null != searchFrom)
            {
                searchFrom.UpdateGridView();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
